<template>
	<Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
		<FormItem prop="userName">
			<Input v-model="form.userName" placeholder="请输入用户名">
			<span slot="prepend">
          <Icon :size="16" type="ios-person"></Icon>
        </span>
			</Input>
		</FormItem>
		<FormItem prop="password">
			<Input type="password" v-model="form.password" placeholder="请输入密码">
			<span slot="prepend">
          <Icon :size="14" type="md-lock"></Icon>
        </span>
			</Input>
		</FormItem>
		<FormItem>
			<Button @click="handleSubmit" type="primary" long>登录</Button>
		</FormItem>
	</Form>
</template>
<script>
	import * as api from '@/api/api.js';
	import Cookies from 'js-cookie';
	export default {
		name: 'LoginForm',
		props: {
			userNameRules: {
				type: Array,
				default: () => {
					return [{
						required: true,
						message: '账号不能为空',
						trigger: 'blur'
					}]
				}
			},
			passwordRules: {
				type: Array,
				default: () => {
					return [{
						required: true,
						message: '密码不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		data() {
			return {
				form: {
					userName: '15198772647',//lan 15198772647 //15887271821
					password: '123456'
				}
			}
		},
		computed: {
			rules() {
				return {
					userName: this.userNameRules,
					password: this.passwordRules
				}
			}
		},
		methods: {
			handleSubmit() {
				this.$refs.loginForm.validate((valid) => {
					if(valid) {
						this.$Spin.show();
						api.postData('login', {
							username: this.form.userName,
							password: this.form.password
						}, this).then((res) => {
							if(res.state == 200) {
								this.$store.commit('setIdentity',res.identity);
								this.$store.commit('setAdministration',res.administration);
								this.$store.commit('setUrlParam');
								Cookies.set('token', res, { expires: 1 })
								Cookies.set('urlParam', "identity="+res.identity+"&administration="+JSON.stringify(res.administration), { expires: 1 })
								sessionStorage.setItem('master',res.identity);
								if(sessionStorage.getItem('roomid')){
									this.$router.push({
										name: 'toolSupervise'
									});
								}else{
									this.$router.push({
										name: 'kRoom'
									});
								}
							} else {
								this.$Notice.error({
									title: '提示',
									desc: res.msg,
								});
							}
							this.$Spin.hide();
						})
					}
				})

			}
		}
	}
</script>